//
//  OSStatusCheck.m
//  AudioUnitDemo
//
//  Created by ilongge on 2021/9/2.
//

#import "OSStatusCheck.h"
#import <AudioToolbox/AudioToolbox.h>
@implementation OSStatusCheck

/**
 检查状态码
 */
+ (void)CheckOSStatus:(OSStatus)status
           andMessage:(NSString *)message
              andExit:(BOOL)exit
{
    if (status != noErr) {
        char statusCode[16];
        *(UInt32 *)statusCode = CFSwapInt32HostToBig(status);
        statusCode[4] = '\0';
        if (isprint(statusCode[0])
            &&
            isprint(statusCode[1])
            &&
            isprint(statusCode[2])
            &&
            isprint(statusCode[3])) {
            NSLog(@"%@ Error: %s", message, statusCode);
        }
        else{
            
            NSString *audioUnitErr = nil;
            switch (status) {
                case kAudioUnitErr_CannotDoInCurrentContext: audioUnitErr = @"kAudioUnitErr_CannotDoInCurrentContext"; break;
                case kAudioUnitErr_FailedInitialization: audioUnitErr = @"kAudioUnitErr_FailedInitialization"; break;
                case kAudioUnitErr_FileNotSpecified: audioUnitErr = @"kAudioUnitErr_FileNotSpecified"; break;
                case kAudioUnitErr_FormatNotSupported: audioUnitErr = @"kAudioUnitErr_FormatNotSupported"; break;
                case kAudioUnitErr_Initialized: audioUnitErr = @"kAudioUnitErr_Initialized"; break;
                case kAudioUnitErr_InvalidElement: audioUnitErr = @"kAudioUnitErr_InvalidElement"; break;
                case kAudioUnitErr_InvalidFile: audioUnitErr = @"kAudioUnitErr_InvalidFile"; break;
                case kAudioUnitErr_InvalidOfflineRender: audioUnitErr = @"kAudioUnitErr_InvalidOfflineRender"; break;
                case kAudioUnitErr_InvalidParameter: audioUnitErr = @"kAudioUnitErr_InvalidParameter"; break;
                case kAudioUnitErr_InvalidProperty: audioUnitErr = @"kAudioUnitErr_InvalidProperty"; break;
                case kAudioUnitErr_InvalidPropertyValue: audioUnitErr = @"kAudioUnitErr_InvalidPropertyValue"; break;
                case kAudioUnitErr_InvalidScope: audioUnitErr = @"kAudioUnitErr_InvalidScope"; break;
                case kAudioUnitErr_NoConnection: audioUnitErr = @"kAudioUnitErr_NoConnection"; break;
                case kAudioUnitErr_PropertyNotInUse: audioUnitErr = @"kAudioUnitErr_PropertyNotInUse"; break;
                case kAudioUnitErr_PropertyNotWritable: audioUnitErr = @"kAudioUnitErr_PropertyNotWritable"; break;
                case kAudioUnitErr_TooManyFramesToProcess: audioUnitErr = @"kAudioUnitErr_TooManyFramesToProcess"; break;
                case kAudioUnitErr_Unauthorized: audioUnitErr = @"kAudioUnitErr_Unauthorized"; break;
                case kAudioUnitErr_Uninitialized: audioUnitErr = @"kAudioUnitErr_Uninitialized"; break;
                case kAudioUnitErr_UnknownFileType: audioUnitErr = @"kAudioUnitErr_UnknownFileType"; break;
                default: audioUnitErr = @"unknown error";
            }
            NSLog(@"%@ Error: %d %@", message, (int)status, audioUnitErr);
        }
        if (exit) {
            //exit(-1);
        }
        
    }
    else{
        NSLog(@"%@ Success", message);
    }
}
@end
